const _ = require('lodash')
const path = require('path')
const glob = require('glob')
const { resolve } = require('../../helper')

module.exports = function(force){

	const { appConf: { src }, command: { modules } } = this
	const getAllModuleConfPath = () => glob.sync(resolve(src, 'modules', '*','conf.json'))
	const moduleConfPath = force ? getAllModuleConfPath() : 
	modules.length > 0 ? 
	modules.map(dir => resolve(src, 'modules', dir, 'conf.json')) : 
	getAllModuleConfPath()

	return moduleConfPath.reduce((pre, cur) => {
		const dir = path.dirname(cur)
		const bundleName = path.basename(dir)
		const bundleConf = require(cur)

		_.transform(bundleConf, (result1, value1, key1) => {
			_.transform(value1, (result2, value2, key2) => {
				pre.push({
					type: 'Normal',
					bundleName,
					moduleName: key1,
					pageName: key2,
					id: `${bundleName}/${key1}/${key2}`,
					request: path.join(dir, key1, key2, 'main'),
					param: value2
				})
			})
		})
		return pre
	}, [])
}